package com.demo.myapplication.adapter;

import android.animation.AnimatorInflater;
import android.animation.StateListAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.util.StateSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.demo.myapplication.R;
import com.demo.myapplication.utils.CommonUtils;

import java.util.ArrayList;
import java.util.Map;

public class RecyclerActionModeAdapter extends RecyclerView.Adapter<RecyclerActionModeAdapter.MyViewHolder> {

  private ArrayList<Map<String, Object>> dataList = new ArrayList<>();
  private MyViewHolder.ClickListener listener;
  private SparseBooleanArray selectedItems;

  public RecyclerActionModeAdapter(ArrayList<Map<String, Object>> dataList) {
    this.dataList.addAll(dataList);
    selectedItems = new SparseBooleanArray();
  }

  public void setListener(MyViewHolder.ClickListener listener) {
    this.listener = listener;
  }

  public SparseBooleanArray getSelectedItems() {
    return selectedItems;
  }

  public int getSelectedItemCount() {
    return selectedItems.size();
  }

  public boolean isSelected(int position) {
    return selectedItems.get(position, false);
  }

  public void clearSelection() {
    selectedItems.clear();
  }

  public void toggleSelection(View v, int position, Drawable mDefaultModeBackgroundDrawable, StateListAnimator mDefaultModeStateListAnimator) {
    boolean isSelected = isSelected(position);
    if (isSelected) {
      selectedItems.delete(position);
    } else {
      selectedItems.put(position, true);
    }
    v.setActivated(!isSelected);
    Log.d("Tag", " position=" + position + ",isactivated=" + v.isActivated());
    switchBackground(v, mDefaultModeBackgroundDrawable, mDefaultModeStateListAnimator);
  }

  private void switchBackground(View itemView, Drawable mDefaultModeBackgroundDrawable, StateListAnimator mDefaultModeStateListAnimator) {
    boolean mIsSelectable = itemView.isActivated();
    Drawable backgroundDrawable = mDefaultModeBackgroundDrawable;

    if (mIsSelectable) {
      backgroundDrawable = getAccentStateDrawable(itemView.getContext());
      Log.d("Tag", "selected accent background=" + backgroundDrawable);
    }
    CommonUtils.setBackgroundDrawable(itemView, backgroundDrawable);
    if (backgroundDrawable != null) {
      backgroundDrawable.jumpToCurrentState();
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      StateListAnimator animator = mDefaultModeStateListAnimator;
      if (mIsSelectable) {
        animator = AnimatorInflater.loadStateListAnimator(itemView.getContext(), R.animator.raise);
      }
      itemView.setStateListAnimator(animator);
      if (animator != null) {
        animator.jumpToCurrentState();
      }
    }
  }

  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_icon_item, parent, false);
    return new MyViewHolder(v, listener);
  }

  private static Drawable getAccentStateDrawable(Context context) {
    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = context.getTheme();
    theme.resolveAttribute(R.attr.colorAccent, typedValue, true);

    Drawable colorDrawable = new ColorDrawable(typedValue.data);

    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[]{android.R.attr.state_activated}, colorDrawable);
    stateListDrawable.addState(StateSet.WILD_CARD, null);

    return stateListDrawable;
  }

  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {
    Map<String, Object> map = dataList.get(position);
    holder.icon.setImageResource((Integer) map.get(CommonUtils.map_icon_key));
    holder.name.setText((String) map.get(CommonUtils.map_text_key));
  }


  @Override
  public int getItemCount() {
    return dataList.size();
  }

  public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
    ImageView icon;
    TextView name;

    private Drawable mDefaultModeBackgroundDrawable;
    private StateListAnimator mDefaultModeStateListAnimator;
    private ClickListener listener;

    public MyViewHolder(View itemView, ClickListener listener) {
      super(itemView);
      icon = (ImageView) itemView.findViewById(R.id.icon);
      name = (TextView) itemView.findViewById(R.id.text);

      mDefaultModeBackgroundDrawable = itemView.getBackground();
      Log.d("Tag", "position=" + getAdapterPosition() + ",defaultBackgroundDrawable=" + mDefaultModeBackgroundDrawable);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mDefaultModeStateListAnimator = itemView.getStateListAnimator();
      }

      this.listener = listener;
      itemView.setOnClickListener(this);
      itemView.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
      if (listener != null) {
        listener.onItemClicked(v, getAdapterPosition(), mDefaultModeBackgroundDrawable, mDefaultModeStateListAnimator);
      }
    }


    @Override
    public boolean onLongClick(View v) {
      if (listener != null) {
        listener.onItemLongClicked(v, getAdapterPosition(), mDefaultModeBackgroundDrawable, mDefaultModeStateListAnimator);
      }
      return true;
    }

    public interface ClickListener {
      void onItemClicked(View v, int position, Drawable mDefaultModeBackgroundDrawable, StateListAnimator mDefaultModeStateListAnimator);

      boolean onItemLongClicked(View v, int position, Drawable mDefaultModeBackgroundDrawable, StateListAnimator mDefaultModeStateListAnimator);
    }
  }
}